/*
 * Ant Group
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.zmdepositdemo.service.manage;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayFundAuthOperationCancelModel;
import com.alipay.api.domain.AlipayFundAuthOperationDetailQueryModel;
import com.alipay.api.domain.AlipayFundAuthOrderAppFreezeModel;
import com.alipay.api.domain.AlipayFundAuthOrderUnfreezeModel;
import com.alipay.api.domain.AlipayTradeOrderinfoSyncModel;
import com.alipay.api.request.AlipayFundAuthOperationCancelRequest;
import com.alipay.api.request.AlipayFundAuthOperationDetailQueryRequest;
import com.alipay.api.request.AlipayFundAuthOrderAppFreezeRequest;
import com.alipay.api.request.AlipayFundAuthOrderUnfreezeRequest;
import com.alipay.api.request.AlipayTradeOrderinfoSyncRequest;
import com.alipay.api.response.AlipayFundAuthOperationCancelResponse;
import com.alipay.api.response.AlipayFundAuthOperationDetailQueryResponse;
import com.alipay.api.response.AlipayFundAuthOrderAppFreezeResponse;
import com.alipay.api.response.AlipayFundAuthOrderUnfreezeResponse;
import com.alipay.api.response.AlipayTradeOrderinfoSyncResponse;
import com.alipay.zmdepositdemo.model.OpenResponse;
import com.alipay.zmdepositdemo.util.AlipayApiCallback;
import com.alipay.zmdepositdemo.util.AlipayApiTemplate;
import com.alipay.zmdepositdemo.util.AlipaySdkUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangjian
 * @version DepositOrderManageServiceImpl.java, v 0.1 2024年06月18日 16:27 zhangjian
 */
@Service
public class DepositOrderManageServiceImpl implements DepositOrderManageService {

    private static final Logger logger = LogManager.getLogger(DepositOrderManageServiceImpl.class);

    private AlipaySdkUtil alipaySdkUtil;

    @Autowired
    public void ConstructorAlipaySdkUtil(AlipaySdkUtil alipaySdkUtil) {
        this.alipaySdkUtil = alipaySdkUtil;
    }

    @Override
    public OpenResponse<AlipayFundAuthOrderAppFreezeResponse> createOrder(AlipayFundAuthOrderAppFreezeModel model) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<AlipayFundAuthOrderAppFreezeResponse, AlipayFundAuthOrderAppFreezeResponse>() {
                    @Override
                    public AlipayFundAuthOrderAppFreezeResponse process() throws AlipayApiException {
                        AlipayFundAuthOrderAppFreezeRequest request = alipayFundAuthOrderAppFreezeRequestBuilder(model);
                        logger.info(
                                "com.alipay.zmdepositdemo.service.manage.DepositOrderManageServiceImpl.createOrder,"
                                        + "AlipayFundAuthOrderAppFreezeRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public AlipayFundAuthOrderAppFreezeResponse getData(AlipayFundAuthOrderAppFreezeResponse response) {
                        return response;
                    }

                    @Override
                    public String getApiName() {
                        return "创建免押订单-alipay.fund.auth.order.app.freeze(线上资金授权冻结接口)";
                    }
                });
    }

    private AlipayFundAuthOrderAppFreezeRequest alipayFundAuthOrderAppFreezeRequestBuilder(
            AlipayFundAuthOrderAppFreezeModel model) {
        AlipayFundAuthOrderAppFreezeRequest request = new AlipayFundAuthOrderAppFreezeRequest();
        request.setBizModel(model);

        return request;

    }

    /**
     * 查询免押订单
     *
     * @param model 请求参数
     * @return 免押订单信息
     */
    @Override
    public OpenResponse<AlipayFundAuthOperationDetailQueryResponse> queryOrder(AlipayFundAuthOperationDetailQueryModel model) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<AlipayFundAuthOperationDetailQueryResponse, AlipayFundAuthOperationDetailQueryResponse>() {
                    @Override
                    public AlipayFundAuthOperationDetailQueryResponse process() throws AlipayApiException {
                        AlipayFundAuthOperationDetailQueryRequest request = alipayFundAuthOperationDetailQueryRequestBuilder(model);
                        logger.info(
                                "com.alipay.zmdepositdemo.service.manage.DepositOrderManageServiceImpl.queryOrder,"
                                        + "AlipayFundAuthOperationDetailQueryRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public AlipayFundAuthOperationDetailQueryResponse getData(AlipayFundAuthOperationDetailQueryResponse response) {
                        return response;
                    }

                    @Override
                    public String getApiName() {
                        return "查询免押订单-alipay.fund.auth.operation.detail.query(资金授权操作查询接口)";
                    }
                });
    }

    /**
     * 查询免押订单
     *
     * @param authNo
     * @param outOrderNo
     * @param operationId
     * @param outRequestNo
     * @param operationType
     */
    @Override
    public OpenResponse<AlipayFundAuthOperationDetailQueryResponse> queryOrder(String authNo, String outOrderNo, String operationId,
                                                                               String outRequestNo, String operationType) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<AlipayFundAuthOperationDetailQueryResponse, AlipayFundAuthOperationDetailQueryResponse>() {
                    @Override
                    public AlipayFundAuthOperationDetailQueryResponse process() throws AlipayApiException {
                        AlipayFundAuthOperationDetailQueryModel model = new AlipayFundAuthOperationDetailQueryModel();
                        model.setAuthNo(authNo);
                        model.setOutOrderNo(outOrderNo);
                        model.setOperationId(operationId);
                        model.setOutRequestNo(outRequestNo);
                        model.setOperationType(operationType);
                        AlipayFundAuthOperationDetailQueryRequest request = alipayFundAuthOperationDetailQueryRequestBuilder(model);
                        logger.info(
                                "com.alipay.zmdepositdemo.service.manage.DepositOrderManageServiceImpl.queryOrder,"
                                        + "AlipayFundAuthOperationDetailQueryRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public AlipayFundAuthOperationDetailQueryResponse getData(AlipayFundAuthOperationDetailQueryResponse response) {
                        return response;
                    }

                    @Override
                    public String getApiName() {
                        return "查询免押订单-alipay.fund.auth.operation.detail.query(资金授权操作查询接口)";
                    }
                });
    }

    private AlipayFundAuthOperationDetailQueryRequest alipayFundAuthOperationDetailQueryRequestBuilder(
            AlipayFundAuthOperationDetailQueryModel model) {
        AlipayFundAuthOperationDetailQueryRequest request = new AlipayFundAuthOperationDetailQueryRequest();
        request.setBizModel(model);

        return request;

    }

    /**
     * 取消免押订单
     *
     * @return boolean true 取消免押订单成功
     */
    @Override
    public OpenResponse<Boolean> cancelOrder(String remark,
                                             String authNo,
                                             String outOrderNo,
                                             String operationId,
                                             String outRequestNo) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<Boolean, AlipayFundAuthOperationCancelResponse>() {
                    @Override
                    public AlipayFundAuthOperationCancelResponse process() throws AlipayApiException {

                        AlipayFundAuthOperationCancelModel model = new AlipayFundAuthOperationCancelModel();
                        model.setRemark(remark);
                        model.setAuthNo(authNo);
                        model.setOutOrderNo(outOrderNo);
                        model.setOperationId(operationId);
                        model.setOutRequestNo(outRequestNo);
                        AlipayFundAuthOperationCancelRequest request = alipayFundAuthOperationCancelRequestBuilder(model);

                        logger.info(
                                "com.alipay.zmdepositdemo.service.manage.DepositOrderManageServiceImpl.cancelOrder,"
                                        + "AlipayFundAuthOperationCancelRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public Boolean getData(AlipayFundAuthOperationCancelResponse response) {
                        return response.isSuccess();
                    }

                    @Override
                    public String getApiName() {
                        return "取消免押订单-alipay.fund.auth.operation.cancel(资金授权撤销接口)";
                    }
                });
    }

    private AlipayFundAuthOperationCancelRequest alipayFundAuthOperationCancelRequestBuilder(AlipayFundAuthOperationCancelModel model) {
        AlipayFundAuthOperationCancelRequest request = new AlipayFundAuthOperationCancelRequest();
        request.setBizModel(model);

        return request;
    }

    /**
     * 同步服务订单信息
     *
     * @return boolean true 同步服务订单信息成功
     */
    @Override
    public OpenResponse<Boolean> syncOrder(String tradeNo,
                                           String outRequestNo,
                                           String bizType,
                                           String origRequestNo,
                                           String orderBizInfo) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<Boolean, AlipayTradeOrderinfoSyncResponse>() {
                    @Override
                    public AlipayTradeOrderinfoSyncResponse process() throws AlipayApiException {

                        AlipayTradeOrderinfoSyncModel model = new AlipayTradeOrderinfoSyncModel();
                        model.setTradeNo(tradeNo);
                        model.setOutRequestNo(outRequestNo);
                        model.setBizType(bizType);
                        model.setOrigRequestNo(origRequestNo);
                        model.setOrderBizInfo(orderBizInfo);

                        AlipayTradeOrderinfoSyncRequest request = alipayTradeOrderinfoSyncRequestBuilder(model);
                        logger.info(
                                "com.alipay.zmdepositdemo.service.manage.DepositOrderManageServiceImpl.syncOrder,"
                                        + "AlipayTradeOrderinfoSyncRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public Boolean getData(AlipayTradeOrderinfoSyncResponse response) {
                        return response.isSuccess();
                    }

                    @Override
                    public String getApiName() {
                        return "同步服务订单信息-alipay.trade.orderinfo.sync(支付宝订单信息同步接口)";
                    }
                });
    }

    private AlipayTradeOrderinfoSyncRequest alipayTradeOrderinfoSyncRequestBuilder(AlipayTradeOrderinfoSyncModel model) {
        AlipayTradeOrderinfoSyncRequest request = new AlipayTradeOrderinfoSyncRequest();
        request.setBizModel(model);
        return request;
    }

    /**
     * 完结免押订单
     *
     * @return boolean true 完结免押订单成功
     */
    @Override
    public OpenResponse<Boolean> finishOrder(String authNo,
                                             String outRequestNo,
                                             String amount,
                                             String remark,
                                             String extraParam) {
        return AlipayApiTemplate.execute(
                new AlipayApiCallback<Boolean, AlipayFundAuthOrderUnfreezeResponse>() {
                    @Override
                    public AlipayFundAuthOrderUnfreezeResponse process() throws AlipayApiException {
                        AlipayFundAuthOrderUnfreezeModel model = new AlipayFundAuthOrderUnfreezeModel();

                        model.setAuthNo(authNo);
                        model.setOutRequestNo(outRequestNo);
                        model.setAmount(amount);
                        model.setRemark(remark);
                        model.setExtraParam(extraParam);

                        AlipayFundAuthOrderUnfreezeRequest request = alipayFundAuthOrderUnfreezeRequestBuilder(model);
                        logger.info(
                                "com.alipay.zmdepositdemo.service.manage.DepositOrderManageServiceImpl.finishOrder,"
                                        + "AlipayFundAuthOrderUnfreezeRequest:" +
                                        JSON.toJSONString(request));
                        return alipaySdkUtil.execute(request);
                    }

                    @Override
                    public Boolean getData(AlipayFundAuthOrderUnfreezeResponse response) {
                        return response.isSuccess();
                    }

                    @Override
                    public String getApiName() {
                        return "完结免押订单-alipay.fund.auth.order.unfreeze(资金授权解冻接口)";
                    }
                });
    }

    private AlipayFundAuthOrderUnfreezeRequest alipayFundAuthOrderUnfreezeRequestBuilder(AlipayFundAuthOrderUnfreezeModel model) {
        AlipayFundAuthOrderUnfreezeRequest request = new AlipayFundAuthOrderUnfreezeRequest();
        request.setBizModel(model);
        return request;
    }

}